<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="团队名称" prop="gname">
      <el-input v-model="ruleForm.gname"></el-input>
    </el-form-item>
    <el-form-item label="负责人员" prop="gleaderName">
      <el-select v-model="ruleForm.gleaderName" placeholder="请选择" >
        <el-option
          v-for="item in options"
          :key="item"
          :label="item"
          :value="item">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="page" prop="page" hidden="true">
      <el-input v-model="page"></el-input>
    </el-form-item>
    <el-form-item label="offset" prop="offset" hidden="true">
      <el-input v-model="offset"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm()">立即创建</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import { insertGroup } from '@/api/group'
  import { getUserList } from '@/api/user'

  export default {
    created() {
      this.getOptions()
      // this.submitForm()
    },
    inject: ['page', 'offset'],
    data() {
      return {
        // value: [],
        options: [],
        ruleForm: {
          gname:'',
          gleaderName:''
        },
        rules: {
          gname: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          gleaderName: [
            { required: true, message: '请输入负责人员', trigger: 'change' }
          ]
        }
      }
    },
    methods: {
      getOptions() {
        let url = '/get/all/user/names'
        getUserList(url).then(res => {
          this.options = JSON.parse(res.data)
        })
      },
      submitForm() {
        // this.$refs.ruleForm.validate((valid) => {
        //   if (valid) {
        insertGroup(this.ruleForm).then((res) => {
          // this.dialogVisible = false
          // 这里是回调函数
          if (res.code != 200) return this.changeUserInfoSuccess('失败', '添加用户失败！')
          this.changeUserInfoSuccess('成功', '添加用户成功！')
          this.clearForm()
          this.getUserData()
        })
        //   } else {
        //     return false
        //   }
        // })
      },
      changeUserInfoSuccess(title, message) {
        this.$notify({
          title: title,
          message: message,
          type: 'success',
          duration: 2000
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      },
      clearForm() {
        this.form.gname = ''
        this.gleaderName = ''
      }
    }
  }
</script>
